package com.amokgames.tictactoe;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TicTacToe extends Activity
//implements ComponentCallbacks
{
	GameEngine gameEngine = new GameEngine();
	Piece pieceButtons[][] = new Piece[3][3];
	
//	@Override
//	public void onConfigurationChanged(Configuration newConfig)
//	{
////		GameEngine gameEngine = this.gameEngine;
////
//	super.onConfigurationChanged(newConfig);
//		//setContentView(R.layout.mainh);
////		
////		this.gameEngine = gameEngine;
//	}
	
	@Override
	protected void onSaveInstanceState (Bundle outState) {
		super.onSaveInstanceState(outState);
		
//		outState.putInt("Turn", 1);
		outState.putString("Turn", gameEngine.turn.name());
		
		for(Integer y = 0; y < 3; y++) {
			for(Integer x = 0; x < 3; x++) {
				outState.putString("Piece" + x.toString() + y.toString(), gameEngine.board[x][y].name());
			}
		}
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
        
    	pieceButtons[0][0] = (Piece)findViewById(R.id.tlPiece);
    	pieceButtons[0][1] = (Piece)findViewById(R.id.tcPiece);
    	pieceButtons[0][2] = (Piece)findViewById(R.id.trPiece);
    	pieceButtons[1][0] = (Piece)findViewById(R.id.mlPiece);
    	pieceButtons[1][1] = (Piece)findViewById(R.id.mcPiece);
    	pieceButtons[1][2] = (Piece)findViewById(R.id.mrPiece);
    	pieceButtons[2][0] = (Piece)findViewById(R.id.blPiece);
    	pieceButtons[2][1] = (Piece)findViewById(R.id.bcPiece);
    	pieceButtons[2][2] = (Piece)findViewById(R.id.brPiece);
        
        for(int y = 0; y < 3; y++)
        	for(int x = 0; x < 3; x++)
        		pieceButtons[x][y].setup(this, gameEngine, x, y);
    	
        Button b = (Button)findViewById(R.id.resetButton);
        b.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
		        reset();
			}
		});
        
        if(savedInstanceState != null) {
        	gameEngine.turn = Turn.valueOf(savedInstanceState.getString("Turn"));
    		for(Integer y = 0; y < 3; y++) {
    			for(Integer x = 0; x < 3; x++) {
    				gameEngine.board[x][y] = Turn.valueOf(savedInstanceState.getString("Piece" + x.toString() + y.toString()));
    			}
    		}
            updateMessage();
        }
        else
        	reset();
    }
    
    public void reset() {
    	gameEngine.reset();
        for(int y = 0; y < 3; y++)
        	for(int x = 0; x < 3; x++)
        		pieceButtons[x][y].reset();
        
        updateMessage();
    }
    
    public void updateMessage() {
        TextView tv = (TextView)findViewById(R.id.textView);

        if(gameEngine.gameOver()) {
	    	if(gameEngine.didPlayerWin(Turn.x))
	    		tv.setText("Player X won!");
	    	else if(gameEngine.didPlayerWin(Turn.o))
	    		tv.setText("Player O won!");
	    	else
	    		tv.setText("Cat's game!");
    	}
        else {
        	if(gameEngine.getTurn() == Turn.x)
        		tv.setText("Player X's turn");
        	else if(gameEngine.getTurn() == Turn.o)
        		tv.setText("Player O's turn");
        }
        
        tv.invalidate();
    }
}